*Insheet and process JOLTS researcher data
cd /OEUS/users/FEDSusers/JOLTS/
set more off
/*
insheet using researcher_12MAY16.txt, clear c 
*Rename variables according to documentation received from Ainslie MacLeod on 12/07/2016
*Revised since variables are differently named but appear to be the same on 2024 04 09
rename schedule id
rename weight weight
rename f_emp aveannemp
rename m_emp emp
rename jo jo
rename n_hires hires
rename quits quits
rename l_d layoffs
rename o_seps othsep
rename t_seps totsep
rename agg_code aggcode
rename nraf nraf
rename weight_f weight_f
rename bmf bmf
rename yrmonth monthinact_everytau_emp_20240409.dta

tostring month, gen(month_str)
gen year = substr(month_str,1,4)
destring year, replace
gen month2 = substr(month_str,5,2)
destring month2, replace
sort year month id
gen mdate = ym(year, month2)
order mdate id 
xtset id mdate, m
drop month month2 year

save researcher_12MAY16, replace

*/
use researcher_12MAY16, clear
set more off
*Take employment as given for now
*This creates three variables.  Between t and t + i:
*1. whether the estab is inactive (inacti)
*2. the quit rate in t
*3. the cumulative hire rate between t and t+i
sort id mdate


foreach i of numlist 1/24{
	gen hires`i' = hires
	gen totsep`i' = totsep
	gen quits`i' = quits
	gen othsep`i' = othsep
	
	*First, create an alternative ntilde for each time span by cumulating flows from t-i to t
	forvalues x = 2/`i'{
		local y = `x' - 1
		*Generate cumulative hires and total seps
		replace hires`i' = hires`i' + L`y'.hires
		replace totsep`i' = totsep`i' + L`y'.totsep
		replace quits`i' = quits`i' + L`y'.quits
		replace othsep`i' = othsep`i' + L`y'.othsep
		
	}
	
	*Now, create lagged employed using cumulative flows
	gen ntilde`i' = emp - (hires`i' - totsep`i') 
	replace ntilde`i' = . if ntilde`i'<0
	gen inactalt_`i' = 0 if emp!=. & ntilde`i'!=. & emp>0 & ntilde`i' >0 
	replace inactalt_`i' = 1 if emp == ntilde`i'  & inactalt_`i' == 0
	
	gen rquits`i' = quits`i'/ntilde`i'
	gen rhires`i' = hires`i'/ntilde`i'
	gen g`i' = 2*(emp - ntilde`i')/(emp + ntilde`i')
}

/*
gen avehires=.
gen avequits=.
gen averhires=.
gen averquits=. 
gen averothsep = .
gen count = .

foreach i of numlist 1/3{
qui{
*sum hires`i' [aw=weight_f], d
*gen hires`i'_t = hires`i'
*replace hires`i'_t = . if hires`i'_t<r(p1)
*replace hires`i'_t = . if hires`i'_t>r(p99)
sum hires`i' [aw=weight_f] if inact`i'==1 & quits`i'>0
replace avehires = r(mean) if _n==`i'
sum ntilde`i' [aw=weight_f] if inact`i'==1 & quits`i'>0
local ntilde=r(mean)

replace averhires = avehires/`ntilde' if _n==`i'

*sum quits`i' [aw=weight_f], d
*gen quits`i'_t = quits`i'
*replace quits`i'_t = . if quits`i'_t<r(p1)
*replace quits`i'_t = . if quits`i'_t>r(p99)
sum quits`i' [aw=weight_f] if inact`i'==1 & quits`i'>0

replace avequits = r(mean) if _n==`i'

replace averquits = avequits/`ntilde' if _n==`i'

replace count = r(N) if _n==`i'
}
}	
*/

save ./data/JOLTS_ntilde, replace
